{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c8aa4072-6a07-4e5f-9f78-6ceb4cd02a36",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import math\n",
    "import serial\n",
    "import serial.tools.list_ports\n",
    "from time import sleep\n",
    "import threading, time\n",
    "# from Thread import event\n",
    "import atexit"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7d04ba29-e63a-4cff-9e3a-90c321b296a8",
   "metadata": {},
   "outputs": [
    {
     "ename": "error",
     "evalue": "OpenCV(4.8.1) :-1: error: (-5:Bad argument) in function 'imshow'\n> Overload resolution failed:\n>  - imshow() missing required argument 'mat' (pos 2)\n>  - imshow() missing required argument 'mat' (pos 2)\n>  - imshow() missing required argument 'mat' (pos 2)\n",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31merror\u001b[0m                                     Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[4], line 32\u001b[0m\n\u001b[0;32m     29\u001b[0m \u001b[38;5;66;03m# 合并红色蒙版\u001b[39;00m\n\u001b[0;32m     30\u001b[0m red_mask \u001b[38;5;241m=\u001b[39m red_mask0 \u001b[38;5;241m+\u001b[39m red_mask1\n\u001b[1;32m---> 32\u001b[0m \u001b[43mcv2\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mimshow\u001b[49m\u001b[43m(\u001b[49m\u001b[43mred_mask\u001b[49m\u001b[43m)\u001b[49m\n",
      "\u001b[1;31merror\u001b[0m: OpenCV(4.8.1) :-1: error: (-5:Bad argument) in function 'imshow'\n> Overload resolution failed:\n>  - imshow() missing required argument 'mat' (pos 2)\n>  - imshow() missing required argument 'mat' (pos 2)\n>  - imshow() missing required argument 'mat' (pos 2)\n"
     ]
    }
   ],
   "source": [
    "dst_path = r'D:\\Project\\PythonProject\\HandCal\\img/1.jpg'\n",
    "\n",
    "img = cv2.imread(dst_path)\n",
    "# 获取原始图像的宽高\n",
    "original_height, original_width = img.shape[:2]\n",
    "# 计算比例\n",
    "scale = min(1920 / original_width, 1280 / original_height)\n",
    "\n",
    "# 计算新宽高\n",
    "new_width = int(original_width * scale)\n",
    "new_height = int(original_height * scale)\n",
    "\n",
    "# resize原始图像，复原宽高比\n",
    "img = cv2.resize(img, (new_width, new_height))\n",
    "\n",
    "img_HSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)\n",
    "\n",
    "# 定义红色范围\n",
    "lower_red = np.array([0, 50, 50])\n",
    "upper_red = np.array([10, 255, 255])\n",
    "# 创建红色蒙版\n",
    "red_mask0 = cv2.inRange(img_HSV, lower_red, upper_red)\n",
    "\n",
    "lower_red = np.array([160, 50, 50])\n",
    "upper_red = np.array([180, 255, 255])\n",
    "# 创建红色蒙版\n",
    "red_mask1 = cv2.inRange(img_HSV, lower_red, upper_red)\n",
    "\n",
    "# 合并红色蒙版\n",
    "red_mask = red_mask0 + red_mask1\n",
    "\n",
    "cv2.imshow('img',red_mask)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "33a60b03-60be-4673-a43f-084ba254c5e1",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
